<?php
if( !defined('IN_INDEX') )
{
	posterror("ACCESS DENIED", "This page is hidden.");
	exit;
}
/*******************************************************************
** sendPM($)
**	Sends a PM to the user with input email
**	This method includes user existance check and if set to do so
**	to send an email to the reciever
** RETURN VALUES
**	false - The PM was not sent
**	true - The PM was sent
**	
*******************************************************************/
function sendPM($to_user_id, $from_user_id, $subject, $body, $reply)
{
	global $db;
	if($to_user_id == 1)
	{
		return false;
	}
	$subject = str_replace("'", "\'", $subject);
	$body = str_replace("'", "\'", $body);

	$sql = "INSERT INTO " . PM_TABLE . " SET ".
		"pm_to='" . $to_user_id . "',".
		"pm_from='" . $from_user_id . "',".
		"pm_subject='" . $subject . "',".
		"pm_body='" . $body . "',".
		"pm_date='" . time() . "'";
	
	if(!$result = $db->sql_query($sql))
	{
		printerror("SQL ERROR", mysql_error());
	}
	else
	{
		if($reply > 0)
		{
			$sql2 = "UPDATE " . PM_TABLE . " SET ".
			"pm_is_read='2' WHERE pm_id='" . $reply . "'";
			if(!$result2 = $db->sql_query($sql2))
			{
				printerror("SQL ERROR", mysql_error());
			}
			else
			{
				return true;
			}
		}
		
		return true;
	}
	return false;
}
/** sendPM() END **/

function listPM_inbox($id, $order_by)
{
	global $db;
	$array = array();
	
	$sql = "SELECT * FROM " . PM_TABLE . " WHERE pm_to='" . $id . "' AND pm_recieverDelete='0' " . $order_by;
	
	if(!$result = $db->sql_query($sql))
	{
		printerror("SQL ERROR", mysql_error());
	}
	else
	{
		while($row = $db->sql_fetchrow($result))
		{
			$array[] = array(
				'id' => $row['pm_id'],
				'to' => getNameFromId($row['pm_to']),
				'to_id' => $row['pm_to'],
				'from' => getNameFromId($row['pm_from']),
				'from_id' => $row['pm_from'],
				'subject' => $row['pm_subject'],
				'body' => $row['pm_body'],
				'date' => $row['pm_date'],
				'is_read' => $row['pm_is_read']
				);
		}
	}
	
	return $array;
}

function listPM_outbox($id, $order_by)
{
	global $db;
	$array = array();
	
	$sql = "SELECT * FROM " . PM_TABLE . " WHERE pm_from='" . $id . "' AND pm_senderDelete='0' " . $order_by;
	
	if(!$result = $db->sql_query($sql))
	{
		printerror("SQL ERROR", mysql_error());
	}
	else
	{
		while($row = $db->sql_fetchrow($result))
		{
			$array[] = array(
				'id' => $row['pm_id'],
				'to' => getNameFromId($row['pm_to']),
				'to_id' => $row['pm_to'],
				'from' => getNameFromId($row['pm_from']),
				'from_id' => $row['pm_from'],
				'subject' => $row['pm_subject'],
				'body' => $row['pm_body'],
				'date' => $row['pm_date'],
				'is_read' => $row['pm_is_read']
				);
		}
	}
	
	return $array;
}

function getPM($id)
{
	global $db;
	$array = array();
	
	$sql = "SELECT * FROM " . PM_TABLE . " WHERE pm_id='" . $id . "'";
	
	if(!$result = $db->sql_query($sql))
	{
		printerror("SQL ERROR", mysql_error());
	}
	else
	{
		while($row = $db->sql_fetchrow($result))
		{
			$array = array(
				'id' => $row['pm_id'],
				'to' => getNameFromId($row['pm_to']),
				'to_id' => $row['pm_to'],
				'from' => getNameFromId($row['pm_from']),
				'from_id' => $row['pm_from'],
				'subject' => $row['pm_subject'],
				'body' => $row['pm_body'],
				'date' => $row['pm_date'],
				'is_read' => $row['pm_is_read']
				);
		}
	}
	
	return $array;
}

function deletePmById($input)
{
	global $db;
	global $CONFIG;
	
	$sql="UPDATE " . PM_TABLE . " SET pm_recieverDelete='1' WHERE pm_id = '" . $input .  "'";
	if(!$result = $db->sql_query($sql))
	{
		printError('SQL_ERROR', mysql_error($sql));
	}
}

function markPmAsRead($input)
{
	global $db;
	global $CONFIG;
	
	$sql="UPDATE " . PM_TABLE . " SET pm_is_read='1' WHERE pm_id = '" . $input .  "'";
	if(!$result = $db->sql_query($sql))
	{
		printError('SQL_ERROR', mysql_error($sql));
	}
}

function deletePmById2($input)
{
	global $db;
	global $CONFIG;
	
	$sql="UPDATE " . PM_TABLE . " SET pm_senderDelete='1' WHERE pm_id = '" . $input .  "'";
	if(!$result = $db->sql_query($sql))
	{
		printError('SQL_ERROR', mysql_error($sql));
	}
}

function countTotalPM($id)
{
	global $db;
	
	$sql = "SELECT * FROM " . PM_TABLE . " WHERE pm_to='" . $id . "'";
	
	if(!$result = $db->sql_query($sql))
	{
		printerror("SQL ERROR", mysql_error());
	}
	else
	{
		return $db->sql_affectedrows();
	}
	
	return 0;
}

function countUnreadPM($id)
{
	global $db;
	
	$sql = "SELECT * FROM " . PM_TABLE . " WHERE pm_to='" . $id . "' AND pm_is_read='0' AND pm_recieverDelete='0'";
	
	if(!$result = $db->sql_query($sql))
	{
		printerror("SQL ERROR", mysql_error());
	}
	else
	{
		return $db->sql_affectedrows();
	}
	
	return 0;
}

function selectAvatarPath($user_id)
{
	global $db;
	global $CONFIG;
	
	$sql = "SELECT user_avatarPath FROM " . USERS_TABLE . " WHERE user_id =". $user_id ."";
	if(!$result = $db->sql_query($sql))
	{
		printError("SQL ERROR", mysql_error($sql));
	}
	
	while($row = mysql_fetch_array($result))
	{
			$val = $row['user_avatarPath'];
	}
	return $val;
}

function checkUserCommented($poster_id, $post_id)
{
	global $db;
	$array = array();
	
	$sql = "SELECT DISTINCT user_id FROM " . COMMENTS_TABLE . " WHERE post_id= '". $post_id ."' AND user_id != '". $poster_id ."' ";
	
	if(!$result = $db->sql_query($sql))
	{
		printError("SQL ERROR", mysql_error($sql));
	}
	else
	{
		while($row = $db->sql_fetchrow($result))
		{
			$array[] = array(
				'user_id' => $row['user_id'],
			);
		}
		return $array;
	}
}

//hungdd
function hook_send_email($to_user_id, $from_user_id, $subject, $body, $reply){
	$TO_USER_PROFILE = new User('id',$to_user_id);
	
	if($TO_USER_PROFILE->getUserEmailNotify() == 1){
		$from = "Treningsplassen<treningsplassen@system.com>";
		$to = $TO_USER_PROFILE->getEmail();
		send($from, $to, $subject, $body);
	}else{
		
	}
	return;
}

function send($from, $to, $subject, $content){
	$headers  = 'MIME-Version: 1.0' . "\r\n";
	$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
	
	$headers .= "To: $to" . "\r\n";
	$headers .= "From: $from" . "\r\n";
	 
	return mail($to, $subject, $content, $headers);
}

?>